package com.android.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.R;
import com.android.bean.Good;
import com.android.bean.ResponseData;
import com.android.bean.User;
import com.bumptech.glide.Glide;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class GoodsDetailsActivity extends AppCompatActivity implements View.OnClickListener{
    private Good goods;
    private User user;

    private ImageView ivHeadPortrait;
    private TextView tvUsrName;
    private TextView tvLocation;
    private TextView tvPrice;
    private TextView tvDesc;
    private ImageView ivPic;
    private Button btnComment;
    private Button btnCollect;
    private Button btnTalk;
    private Button btnCart;

    private String headPortrait;
    private String usrName;
    private String location;
    private String price;
    private String desc;
    private String picUrl;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);
        //注册EventBus
        EventBus.getDefault().register(this);

        initData();
        initView();
        //点击购物车按钮
        purchase();
    }

    private void purchase() {
        btnCart.setOnClickListener(this);
    }

    private void initData() {
        if (goods != null){
            usrName = goods.getUsername();
            location = goods.getAddr();
            price = goods.getPrice().toString();
            desc = goods.getContent();
            if(goods.getImageUrlList().size()>0){
                picUrl = goods.getImageUrlList().get(0).toString();

            }

        }

    }

    private void initView() {
        tvUsrName = findViewById(R.id.tv_username);
        tvLocation = findViewById(R.id.tv_location);
        tvPrice = findViewById(R.id.tv_price);
        tvDesc = findViewById(R.id.tv_desc);
        ivPic = findViewById(R.id.iv_pic);
        btnCart = findViewById(R.id.btn_buy_cart);
        btnComment = findViewById(R.id.btn_comment);
        btnCollect = findViewById(R.id.btn_collect);
        btnTalk = findViewById(R.id.btn_talk);

        if (usrName != null)
            tvUsrName.setText(usrName);
        if (location != null)
            tvLocation.setText(location);
        if (price != null)
            tvPrice.setText(price);
        if (desc != null)
            tvDesc.setText(desc);
        if (picUrl != null){
            Glide.with(this)
                    .load(picUrl)
                    .into(ivPic);
        }



    }

    //拿取点进来的商品数据
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getGoods(Good data){
        if(data != null){
            this.goods = data;
//            Log.e("Goods",goods.getImageUrlList().get(0).toString());
        }else {
            Log.e("Goods","获取失败");

        }
    }
    //拿取点进来的用户数据
    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)
    public void getLoginUser(ResponseData<User> responseData){
        this.user = responseData.getData();
    }




    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_buy_cart:
                Intent intent = new Intent(this,ConfirmPurchaseActivity.class);
                startActivity(intent);
                break;
        }
    }
}